package com.ykt.service;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.ykt.common.constant.ParamNames;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;
import com.ykt.model.Goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 商品服务
 *
 * @author long.hua
 * @version 1.0.0
 * @since 2016-02-15 22:46
 */
public class GoodsService {

    private Goods goodsDao = Goods.dao;


    @Before(Tx.class)
    public void changePrice(int goodsId, double price) {
        goodsDao.set(Goods.id, goodsId);
        goodsDao.set(Goods.sale_price, price);
        goodsDao.update();
    }

    @Before(Tx.class)
    public void changeStatus(int goodsId, String status) {
        goodsDao.set(Goods.id, goodsId);
        goodsDao.set(Goods.status, status);
        goodsDao.update();
    }

    public PageData<Goods> pageFindByShopId(PageParam<Map<String, Object>> param) {
        return goodsDao.paginate(param);
    }

    public List<Goods> findByShopId(int shopId, String status){
        Map<String, Object> param = new HashMap<>();
        param.put(ParamNames.shopId, shopId);
        param.put(ParamNames.status, status);

        PageParam<Map<String, Object>> pageParam = new PageParam<>();
        pageParam.setOffset(0);
        pageParam.setPageSize(Integer.MAX_VALUE);
        pageParam.setP(param);

        PageData<Goods> pd = goodsDao.paginate(pageParam);
        if(pd != null){

            return pd.getPageData();
        }

        return null;
    }
}
